var datagrid1;
var editRow = undefined;
var rowEditor = undefined;
$(function ()
{
    var authorityString=getAuthorityByMenuName("用户管理");
    //根据权限设置按钮的显示和隐藏---custom_Common.js
    IsNoneOrBlock(authorityString,"Role");

    datagrid1 = $("#dg").datagrid(
        {
            url : "../../api/nj_getAllUserInfoBypage",
            method : "get",
            isField : "roleid",
            singleSelect : true, //设置单选
            toolbar : '#toolBar',
            editors : false,
            pagination : true, //显示分页
            pageSize : 10, //分页大小
            pageList : [5, 10, 15, 20], //每页的个数
            beforePageText : '第', //页数文本框前显示的汉字
            afterPageText : '页    共 {pages} 页',
            displayMsg : '当前显示 {from} - {to} 条记录   共 {total} 条记录',
            fit : true, //自动补全
            fitColumns : true,
            title : "用户管理",
            columns : [[//每个列具体内容
                    {
                        field : 'ck',
                        checkbox : true
                    },

                    {
                        field : 'id',
                        title : 'ID',
                        width : 100,
                        hidden : true,
                        align : 'center'
                    },
                    {
                        field : 'realname',
                        title : '真实姓名',
                        width : 100,
                        align : 'center'
                    },
                    {
                        field : 'userno',
                        title : '工号',
                        width : 100,
                        align : 'center'     
                    },

                    {
                        field : 'loginname',
                        title : '登入名',
                        width : 100,
                        align : 'center'    
                    },
                    {
                        field : 'password',
                        title : '密码',
                        width : 100,
                        hidden : true,
                        align : 'center'        
                    },
                    {
                        field : 'phone',
                        title : '电话',
                        width : 120,
                        align : 'center'  
                    },
                    {
                        field : 'email',
                        title : '邮箱',
                        width : 180,
                        align : 'center'
                    },
                    {
                        field : 'departmentname',
                        title : '部门',
                        width : 150,
                        align : 'center'
                    },
                    {
                        field : 'rolename',
                        title : '角色',
                        width : 120,
                        align : 'center'
                    },
                    {
                        field : 'state',
                        title : '状态',
                        width : 50,
                        align : 'center',
                        formatter:function(value , record , index){
                            if(value == 0){
                                return '<span style=color:red; >离职</span>' ;
                            } else if( value == 1){
                                return '<span style=color:green; >在职</span>' ; 
                            }else{
                                return '<span style=color:blue; >禁用</span>' ; 
                            }
                        }
                    },
                    {
                        field : 'adduser',
                        title : '添加人',
                        width : 100,
                        align : 'center'
                    },
                    {
                        field : 'addtime',
                        title : '添加时间',
                        width : 120,
                        align : 'center',
                        formatter:function(val,row){
                          var now = new Date(val);
                          return now.format("yyyy-MM-dd hh:mm:ss");
                        }
                     }
                ]],

            onAfterEdit : function (rowIndex, rowData, changes)
            {
                var inserted = datagrid1.datagrid('getChanges', 'inserted');
                var updated = datagrid1.datagrid('getChanges', 'updated');
                if (inserted.length < 1 && updated.length < 1)
                {
                    editRow = undefined;
                    datagrid1.datagrid('unselectAll');
                    return;
                }

                var url = '';

                if (inserted.length > 0)
                {
                    url = '../../api/saveRoleInfo';
                }

                if (updated.length > 0)
                {
                    url = '../../api/updateRoleInfo';
                }
                $.ajax(
                {
                    url : url,
                    data : rowData,
                    dataType : 'json',
                    success : function (r)
                    {

                        if (r == 1)
                        {
                            datagrid1.datagrid('acceptChanges');
                            // $.messager.show({
                            //  msg : r.msg,
                            //  title : '成功'
                            // });
                            editRow = undefined;
                            datagrid1.datagrid('reload');
                        }
                        else if (r == 2)
                        {
                            datagrid1.datagrid('acceptChanges');
                            editRow = undefined;
                            //datagrid1.datagrid('reload',);
                            $.messager.alert('警告', '角色名称已存在！', 'warning');
                            datagrid1.datagrid('reload', '../../api/getAllRoleInfoBypageClient');
                        }
                        else
                        {
                            /*datagrid.datagrid('rejectChanges');*/
                            datagrid1.datagrid('beginEdit', editRow);
                            $.messager.alert('错误', '角色操作失败！', 'error');
                        }
                        datagrid1.datagrid('unselectAll');
                    }
                }
                );
            }
         
        }
        );

    //增加
    $("#addUser").click(function ()
    {
        $('#editinfo').form('clear');
        $('#userno').attr("readonly", false);
        $('#loginname').attr("readonly", false);
        $('#manage2').hide();
        $('#manage1').show();
        $('#editinfo').dialog('setTitle', '增加');
        $('#editinfo').window('open').window('move',
        {
            // left:450,
            top : 100
        }
        );
        $("#editinfo").form("load",
        {
            adduser : realname,
        }
        );

    }
    );

    //删除
    $("#deleteUser").click(function ()
    {
        var rows = datagrid1.datagrid('getSelections');

        if (rows.length <= 0)
        {
            $.messager.alert('警告', '您没有选择', 'warning');
        }
        else if (rows.length > 1)
        {
            $.messager.alert('警告', '不支持批量删除', 'warning');
        }
        else
        {
            var index = datagrid1.datagrid('getRowIndex', datagrid1.datagrid('getSelected'))
                var row1 = datagrid1.datagrid('getData').rows[index];
            $.messager.confirm('确定', '您确定要删除吗', function (t)
            {
                if (t)
                {

                    $.ajax(
                    {

                        url : '../../api/nj_deleteUserInfo?id=' + row1.userid,
                        success : function (r)
                        {
                            if (r == 1)
                            {
                                datagrid1.datagrid('acceptChanges');
                                editRow = undefined;
                                datagrid1.datagrid('reload');
                            }
                            else
                            {
                                datagrid1.datagrid('beginEdit', editRow);
                                $.messager.alert('错误', "删除用户信息失败", 'error');
                            }
                            datagrid1.datagrid('unselectAll');
                        }

                    }
                    );

                }
            }
            )
        }
    }
    );
    //修改
    $("#modifyUser").click(function ()
    {

        var rows = datagrid1.datagrid('getSelections');
        if (rows.length <= 0)
        {
            $.messager.alert('警告', '您没有选择', 'warning');
        }
        else if (rows.length == 1)
        {
            var index = datagrid1.datagrid('getRowIndex', datagrid1.datagrid('getSelected'));
            var row1 = datagrid1.datagrid('getData').rows[index];
            // $('input').attr("readonly","readonly")//将input元素设置为readonly
            　 // $('input').attr("readonly","")//去除input元素的readonly属性
            // $.messager.alert('警告',row1.passwordperiod);
            $('#userno').attr("readonly", true);
            $('#loginname').attr("readonly", true);
            $('#manage1').hide();
            $('#manage2').show();
            $('#editinfo').form('clear');
            $('#editinfo').dialog('setTitle', '修改');
            $('#editinfo').window('open').window('move',
            {
                // left:450,
                top : 100
            }
            );
            // document.getElementById('departmentnameShow').value=row1.departmentname;
            // document.getElementById('passwordperiod').value=row1.passwordperiod;
            // passwordperiod:row1.passwordperiod,
            //设置部门下拉框树型组件值
            $('#treede').combotree('setValue', row1.departmentid);
            $('#treede').combotree('setText', row1.departmentname);
            editUrl = "../../api/updateUserInfo";
            $("#editinfo").form("load",
            {

                userno : row1.userno,
                loginname : row1.loginname,
                password : row1.password,
                phone : row1.phone,
                email : row1.email,
                realname : row1.realname,
                roleid : row1.roleid,
                state : row1.state,
                businessDepartmentName : row1.seconddepartmentname,
                businessDepartmentid : row1.seconddepartmentid,
                passwordperiod : row1.passwordperiod,
                // departmentid:row1.departmentid,


                // historypassword1:row1.historypassword1,
                // historypassword2:row1.historypassword2,
                // adduser:row1.adduser,
                // addtime:row1.addtime,
                userid : row1.userid,

            }
            );

        }
        else
        {
            $.messager.alert('警告', '不支持批量修改', 'warning');
        }

    }
    );

}
);

//整体模糊查询
function searchUsers()
{
    var searchusername = $('#searchusername').textbox('getValue');
    var searchuserno = $('#searchuserno').textbox('getValue');
    //reload形式加载模糊查询
    $('#dg').datagrid('reload', '../../api/nj_getAllUserInfo?realname=' + searchusername + '&userno=' + searchuserno);
}

//通过选择所属事业部来获取所属事业部的ID
$(document).ready(function ()
{
    $('#layout').layout('expand', 'north');

    //获取所有的角色信息
    $('#roleid').combobox(
    {
        multiple : false,
        valueField : 'roleid',
        mode : 'remote',
        textField : 'rolename',
        editable : false,
        url : '../../api/nj_getAllRoleInfo',
        method : 'get',
        fitColumns : true
    }
    );

    $('#adduser').combobox(
    {
        multiple : false,
        valueField : 'realname',
        mode : 'remote',
        textField : 'realname',
        editable : false,
        url : '../../api/getAllUserInfo',
        method : 'get',
        fitColumns : true

    }
    );
    //获取所有的部门信息
    $('#treede').combotree(
    {
        url : '../../api/nj_getAllDepartmentStructure',
        multiple : false,
        mode : 'remote',
        fitColumns : true,
        editable : false,
        method : 'get',
        valueField : 'id',
        textField : 'text',
    }
    );

    //保存用户基本信息
    $("#save").click(function ()
    {
        var userno = document.getElementById("userno").value;
        var realname = document.getElementById("realname").value;
        var loginname = document.getElementById("loginname").value;
        var password = document.getElementById("password").value;
        // var businessDepartmentName=document.getElementById("businessDepartmentName").value;
        // var businessDepartmentid=document.getElementById("businessDepartmentid").value;
        // var departmentid=document.getElementById("departmentid").value;
        var departmentid = $('#treede').combotree('getValue');
        // $.messager.alert('警告',departmentid);
        // var businessDepartmentName=$('#businessDepartmentName').combobox('getValues');
        var roleid = $('#roleid').combobox('getValues');
        // var departmentid=$('#departmentid').combobox('getValues');
        // var adduser=$('#adduser').combobox('getValues');
        var state = $('#state').combobox('getValues');
        if (userno == "" || userno == null || realname == "" || realname == null ||
            loginname == "" || loginname == "" || password == "" || password == "" ||
            roleid == "" || roleid == null || departmentid == "" || departmentid == null ||
            state == "" || state == null)
        {
            $.messager.alert('警告', '工号、登录名、姓名、密码、角色、部门和状态不能为空！', 'warning');
        }
        else
        {

            var date = new Date();
            var y = date.getFullYear();
            var m = date.getMonth() + 1;
            var d = date.getDate();
            var h = date.getHours();
            var min = date.getMinutes();
            var s = date.getSeconds();
            var nowtime = y + '-' + (m < 10 ? ('0' + m) : m) + '-' + (d < 10 ? ('0' + d) : d) + ' ' + (h < 10 ? ('0' + h) : h) + ':' + (min < 10 ? ('0' + min) : min) + ':' + (s < 10 ? ('0' + s) : s);

            roleid = Number(roleid);
            departmentid = Number(departmentid);
            state = Number(state);
            // adduser=adduser+"";
            adduser = realnametrue;
            // var addtime = $('#addtime').datetimebox('getValue');
            var addtime = nowtime;
            var passwordperiod = $('#passwordperiod').datetimebox('getValue');
            passwordperiod = passwordperiod + "";
            var email = document.getElementById("email").value;
            var phone = document.getElementById("phone").value;
            var emailString = email.toString();
            if (emailString.indexOf("@") >= 0 || email == "" || email == null)
            {

                $.ajax(
                {
                    type : "get",
                    dataType : "json",
                    url : "../../api/saveUserInfo",
                    data :
                    {
                        /*userid:userid,*/
                        userno : userno,
                        loginname : loginname,
                        password : password,
                        phone : phone,
                        email : email,
                        realname : realname,
                        departmentid : departmentid,
                        roleid : roleid,
                        state : state,
                        passwordperiod : passwordperiod,
                        adduser : adduser,
                        addtime : addtime
                    },

                    success : function (r)
                    {
                        if (r == 1)
                        {
                            $('#editinfo').dialog('close');

                        }
                        else if (r == 3)
                        {

                            $.messager.alert('警告', '工号已存在', 'warning');
                        }
                        else if (r == 2)
                        {

                            $.messager.alert('警告', '登录名已存在', 'warning');
                        }
                        else
                        {
                            $.messager.alert('错误', '用户添加失败', 'error');
                        }
                    }

                }
                );
            }
            else
            {
                $.messager.alert('警告', '邮箱格式错误，请重新输入。', 'warning');
            }
        }
    }
    );
    
    $("#cancel").click(function ()
    {
        $('#editinfo').dialog('close');
    }
    );
}
);



//选择部门按钮功能
$('#selectTreeDept').click(function ()
{
    $('#treebox').tree(
    {
        url : '../../api/GetTreedepInfosBypid',
        method : "get",
        lines : true,
    }
    );

    $('#selectTreeDeptDiv').window('open').window('move',
    {
        left : 650,
        top : 150
    }
    )
}
);


